package cn.jb698.codec.msm.generator.v5base;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jb698.codec.msm.generator.util.CodecCfgObjUtils;
import cn.jb698.codec.msm.generator.util.CodecDbConnectionUtils;
import cn.jb698.codec.msm.generator.util.CodecGeneUtils;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgColumnObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgTableObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudConfigBean;

/**
 * 创建Json配置-Mysql的实现
 * 
 * @author SDPC_LIU
 *
 */
public class BcrudCfgObjCreateServiceMysqlImpl implements BcrudCfgObjCreateService {

	private static final Logger logger = LoggerFactory.getLogger(BcrudCfgObjCreateServiceMysqlImpl.class);

	/**
	 * 根据配置构建一条连接请求
	 * @param bcrudConfigBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private Connection connectionGenerate(BcrudConfigBean bcrudConfigBean) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		String dbDriverClassName = bcrudConfigBean.getDbDriverClassName();
		String dbConnectionUrl = bcrudConfigBean.getDbConnectionUrl();
		String dbUserName = bcrudConfigBean.getDbUserName();
		String dbPassword = bcrudConfigBean.getDbPassword();
		Class.forName(dbDriverClassName).newInstance();
		Connection currConnection = DriverManager.getConnection(dbConnectionUrl, dbUserName, dbPassword);
		return currConnection;
	}

	/**
	 * 计算并补充字段
	 * 表及字段配置-计算并补充
	 * @param crudCfgBean  请求对象
	 * @param connection 库连接
	 * @param dbName 库名称
	 * @param cfgTable 表配置
	 * @return
	 * @throws SQLException
	 */
	public BcrudCfgTableObj doBcrudCfgTableObjAndColumnsCalcAndFill(BcrudConfigBean crudCfgBean, Connection connection, String dbName, BcrudCfgTableObj cfgTable) throws SQLException {
		
		String basePackage = crudCfgBean.getBasePackageName();
		String projPackageName = crudCfgBean.getProjPackageName();

		String[] primKeyColumns = cfgTable.getPrimKeyColumns();
		String tableName = cfgTable.getTableName();
		String[] ignoreColumns = cfgTable.getIgnoreColumns();
		String defEntityObjName = cfgTable.getDefEntityObjName();
		boolean hasAutoCutLengh = cfgTable.getHasAutoCutLengh();
		boolean hasPersistence = cfgTable.getHasPersistence();
		boolean hasBaomidou = cfgTable.getHasBaomidou();
		boolean hasSwaggerUi = cfgTable.getHasSwaggerUi();
		cfgTable.setDbType("mysql");

		if(StringUtils.isEmpty(dbName)) {
			dbName = CodecDbConnectionUtils.getDatabaseName(connection);
		}

		if (basePackage == null) {
			throw new IllegalArgumentException("基础包不能为空");
		}

		DatabaseMetaData databaseMetaData = connection.getMetaData();
		List<String> pkColumnNameList = new ArrayList<String>();
		String tableComment = null; // 表级别字段注释
		
		// 提取主键
		if (primKeyColumns != null && primKeyColumns.length > 0) {
			// 有指定主键字段,则按指定的主键字段来生成
			for (String pkColumn : primKeyColumns) {
				pkColumnNameList.add(pkColumn);
			}
		} else {
			// 否则从表里读取主键信息
			ResultSet pkColumnResultSet = databaseMetaData.getPrimaryKeys(connection.getCatalog(), null, tableName); // 取主键
			while (pkColumnResultSet.next()) {
				String columnName = pkColumnResultSet.getString("COLUMN_NAME");
				pkColumnNameList.add(columnName);
			}
		}
		logger.debug("解析后的主键名称列表:" + pkColumnNameList);

		// 提取表注释
		String createTableDdl = null ;
		if (tableComment == null || "".contentEquals(tableComment)) {
			ResultSet rs = connection.createStatement().executeQuery("SHOW CREATE TABLE " + tableName);
			if (rs != null && rs.next()) {
				String createDDL = rs.getString(2); // 建表的DDL
				createTableDdl = createDDL;
				int index = createDDL.lastIndexOf("COMMENT='");
				if (index > 0) {
					tableComment = createDDL.substring(index + 9);
					tableComment = tableComment.substring(0, tableComment.length() - 1);
				}
			}
		}

		// 提取字段及数据结构(后期考虑从DDL里读取)
		List<BcrudCfgColumnObj> allColumnsList = null; // 所有字段及注释

		{
			// 取字段及数据类型,
			allColumnsList = doOneTableAllColumnsCalc(connection , dbName, cfgTable );
		}

		// 提取自增字段
		List<String> autoIncrementColumnNameList = new ArrayList<>();
		Map<String,Object> autoIncrementColumnNameMap = new HashMap<>(pkColumnNameList.size());
		{
			for (BcrudCfgColumnObj tmpColumnObj : allColumnsList) {
				Boolean tmp_isAutoIncrement = tmpColumnObj.getIsAutoIncrement();
				String tmp_column = tmpColumnObj.getColumn();
				if(tmp_isAutoIncrement != null && tmp_isAutoIncrement && pkColumnNameList.size() == 1  && pkColumnNameList.get(0).equals(tmp_column) ) {
					autoIncrementColumnNameList.addAll(pkColumnNameList);
				}
			}
		}
		if(!autoIncrementColumnNameList.isEmpty()) {
			for (String tmpColumnName : autoIncrementColumnNameList) {
				autoIncrementColumnNameMap.put(tmpColumnName, tmpColumnName);
			}
		}

		logger.debug("取的所有字段映射信息:" + allColumnsList);
		
		// PK字段列表
		List<BcrudCfgColumnObj> pkColumnsList = new ArrayList<BcrudCfgColumnObj>();
		
		// 其它字段列表
		List<BcrudCfgColumnObj> otherColumnsList = new ArrayList<BcrudCfgColumnObj>();
		
		Map<String, Boolean> pkColumnNameMap = new HashMap<String, Boolean>();
		for (String pkColumnName : pkColumnNameList) {
			pkColumnNameMap.put(pkColumnName, true);
		}
		
		for (BcrudCfgColumnObj tmpColumn : allColumnsList) {
			String tmpColumnName = tmpColumn.getColumn();
			Boolean isPk = pkColumnNameMap.get(tmpColumnName);
			if (isPk != null && isPk) {
				pkColumnsList.add(tmpColumn);
				Object tmp_autoIncrementColumn = autoIncrementColumnNameMap.get(tmpColumnName);
				if(tmp_autoIncrementColumn != null ) {
					tmpColumn.setIsAutoIncrement(true);
				}
				tmpColumn.setIsPrimKey(isPk);
			} else {
				otherColumnsList.add(tmpColumn);
			}
		}

		if (ignoreColumns != null && ignoreColumns.length > 0) {
			Map<String, String> ignoreColumnsMap = new HashMap<>(ignoreColumns.length);
			for (String ignoreColumn : ignoreColumns) {
				ignoreColumnsMap.put(ignoreColumn, ignoreColumn);
			}
			
			List<BcrudCfgColumnObj> tgtOtherColumnList = new ArrayList<>();
			for (BcrudCfgColumnObj tmp_columnJsBean : otherColumnsList) {
				String tmp_column = tmp_columnJsBean.getColumn();
				String tmp_ignoreColumn = ignoreColumnsMap.get(tmp_column);
				if (tmp_ignoreColumn != null && !"".equals(tmp_ignoreColumn)) {
					continue;
				}
				tgtOtherColumnList.add(tmp_columnJsBean);
			}
			otherColumnsList = tgtOtherColumnList;
		}

		// 表别名:
		cfgTable.setBasePackage(basePackage);
		cfgTable.setProjPackage(projPackageName);
		cfgTable.setPkColumns(pkColumnsList);
		cfgTable.setCreateTableDdl(createTableDdl);
		cfgTable.setComment(tableComment);
		cfgTable.setOtherColumns(otherColumnsList);
		if ( Objects.nonNull(defEntityObjName)) {
		    // 自定义Bean名称的支持
		    cfgTable.getFileObjEntity().setObjName(defEntityObjName);
		}
		cfgTable.setHasSwaggerUi(hasSwaggerUi);
		cfgTable.setHasPersistence(hasPersistence);
		cfgTable.setHasBaomidou(hasBaomidou);
		cfgTable.setHasAutoCutLengh(hasAutoCutLengh);
		return cfgTable;
	}

	/**
	 * 一张表的所有字段计算
	 * @param connection
	 * @param dbName
	 * @param cfgTable
	 * @return
	 * @throws SQLException
	 */
	private List<BcrudCfgColumnObj> doOneTableAllColumnsCalc(Connection connection, String dbName, BcrudCfgTableObj cfgTable) throws SQLException {

		List<BcrudCfgColumnObj> rtList = new ArrayList<>();
		String tableName = cfgTable.getTableName();
		String calcSql = " SELECT t.* "
				+ "FROM `information_schema`.`COLUMNS` t "
				+ "WHERE 1=1 "
				+ " AND t.`TABLE_SCHEMA` = '"+dbName+"' "
				+ " AND  t.`TABLE_NAME` = '"+tableName+"' "
				+ " ORDER BY t.`ORDINAL_POSITION` ASC "
				;
		ResultSet descRst = connection.createStatement().executeQuery(calcSql);
		while(descRst.next()) {
			String tmp_columnName = descRst.getString("COLUMN_NAME");
//			String tmp_isNullable = descRst.getString("IS_NULLABLE");
			String tmp_dataType = descRst.getString("DATA_TYPE"); // bigint , int , tinyint , varchar , decimal , date , timestamp , datetime, 
			Long tmp_charMaxLength = descRst.getLong("CHARACTER_MAXIMUM_LENGTH"); // 字段长度
			Long tmp_numericPrecision = descRst.getLong("NUMERIC_PRECISION"); // 数字长度
			Long tmp_numericScale = descRst.getLong("NUMERIC_SCALE"); // 数字精度
			String tmp_columnKey = descRst.getString("COLUMN_KEY"); // PRI , UNI , KEY , 
			String tmp_columnType = descRst.getString("COLUMN_TYPE"); // 字段类型表达式,例 VARCHAR(64) 
			String tmp_columnComment = descRst.getString("COLUMN_COMMENT"); // 字段注解
			String tmp_extra = descRst.getString("EXTRA"); // 扩展说明,例: auto_increment , DEFAULT_GENERATED on update CURRENT_TIMESTAMP , 
			
			{
				
				String jdbcType = null;
				String javaType = null;
				String golangType = null; // golang的类型 
				String columnTypeStr = null;
				Boolean isAutoIncrement = false; 
				if (
					   CodecCfgObjUtils.columnTypeName_BIT.equalsIgnoreCase(tmp_dataType)
					|| CodecCfgObjUtils.columnTypeName_TINYINT.equalsIgnoreCase(tmp_dataType)
					|| CodecCfgObjUtils.columnTypeName_SMALLINT.equalsIgnoreCase(tmp_dataType)
					|| CodecCfgObjUtils.columnTypeName_INT.equalsIgnoreCase(tmp_dataType)
					|| CodecCfgObjUtils.columnTypeName_INTEGER.equalsIgnoreCase(tmp_dataType)
						
				) {
					if (CodecCfgObjUtils.columnTypeName_BIT.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_BIT;
					} else if (CodecCfgObjUtils.columnTypeName_TINYINT.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_TINYINT;
					} else if (CodecCfgObjUtils.columnTypeName_INTEGER.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_INTEGER;
					} else if (CodecCfgObjUtils.columnTypeName_INT.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_INT;
					}else if (CodecCfgObjUtils.columnTypeName_SMALLINT.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_SMALLINT;
					}
					jdbcType = CodecCfgObjUtils.jdbcType_INTEGER;
					javaType = CodecCfgObjUtils.javaType_Integer;
					golangType = CodecCfgObjUtils.golangType_Integer;
					
				} else if (CodecCfgObjUtils.columnTypeName_BIGINT.equalsIgnoreCase(tmp_dataType)) {
					columnTypeStr = CodecCfgObjUtils.columnTypeName_BIGINT;

					jdbcType = CodecCfgObjUtils.jdbcType_INTEGER;
					javaType = CodecCfgObjUtils.javaType_Long;
					golangType = CodecCfgObjUtils.golangType_Long;
					
				} else if (
					       CodecCfgObjUtils.columnTypeName_DECIMAL.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_DOUBLE.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_FLOAT.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_REAL.equalsIgnoreCase(tmp_dataType)
						) {
					// 浮点数据处理
					if (CodecCfgObjUtils.columnTypeName_DECIMAL.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_DECIMAL;
					} else if (CodecCfgObjUtils.columnTypeName_DOUBLE.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_DOUBLE;
					} else if (CodecCfgObjUtils.columnTypeName_FLOAT.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_FLOAT;
					} else if (CodecCfgObjUtils.columnTypeName_REAL.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_REAL;
					}
					jdbcType = CodecCfgObjUtils.jdbcType_DECIMAL;

					javaType = CodecCfgObjUtils.javaType_Double;
					String cfg_javaTypeDouble = cfgTable.getJavaTypeDouble();
					if(cfg_javaTypeDouble != null && CodecCfgObjUtils.javaType_BigDecimal.equals(cfg_javaTypeDouble) ) {
						javaType = CodecCfgObjUtils.javaType_BigDecimal;
					}
					golangType = CodecCfgObjUtils.golangType_Double;
				} else if (
					       CodecCfgObjUtils.columnTypeName_DATE.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_TIME.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_TIMESTAMP.equalsIgnoreCase(tmp_dataType)
						|| CodecCfgObjUtils.columnTypeName_DATETIME.equalsIgnoreCase(tmp_dataType)
						
						) {
				    // 时间计算
					if ( CodecCfgObjUtils.columnTypeName_DATE.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_DATE;
						jdbcType =CodecCfgObjUtils. jdbcType_DATE;
					} else if (CodecCfgObjUtils.columnTypeName_TIME.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_TIME;
						jdbcType = CodecCfgObjUtils.jdbcType_TIMESTAMP;
					} else if (CodecCfgObjUtils.columnTypeName_TIMESTAMP.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_TIMESTAMP;
						jdbcType = CodecCfgObjUtils.jdbcType_TIMESTAMP;
					}else if (CodecCfgObjUtils.columnTypeName_DATETIME.equalsIgnoreCase(tmp_dataType)) {
						columnTypeStr = CodecCfgObjUtils.columnTypeName_DATETIME;
						jdbcType = CodecCfgObjUtils.jdbcType_TIMESTAMP;
					}
					javaType = CodecCfgObjUtils.javaType_Date;
					golangType = CodecCfgObjUtils.golangType_Date;
				} else {
					// 默认为字符串
					columnTypeStr = CodecCfgObjUtils.columnTypeName_VARCHAR;
					jdbcType = CodecCfgObjUtils.jdbcType_VARCHAR;
					javaType = CodecCfgObjUtils.javaType_String;
					golangType = CodecCfgObjUtils.golangType_String;

					if(tmp_columnType.toUpperCase().indexOf("JSON") >= 0 ) {
					    columnTypeStr =  CodecCfgObjUtils.columnTypeName_JSON;
					}else if(tmp_columnType.toUpperCase().indexOf("TEXT") >= 0 ) {
					    columnTypeStr =  CodecCfgObjUtils.columnTypeName_TEXT;
                    }
				}
				if(StringUtils.isBlank(columnTypeStr)) {
					throw new IllegalArgumentException("类型计算异常(" + tmp_dataType + ")");
				}

				if("auto_increment".equals(tmp_extra)) {
					isAutoIncrement = true;
				}
				
				logger.debug("dataType(" + tmp_dataType + ")转化值:columnTypeStr("+columnTypeStr+"),jdbcType(" + jdbcType + ")"+",javaType(" + javaType + "),golangType("+golangType+")" );
				

	            String tmp_propName = CodecGeneUtils.parseColumnNameToPropName(tmp_columnName); // 属性名称.
	            
	            Boolean isPrimKey = Boolean.FALSE;
	            if(Objects.nonNull(tmp_columnKey) && "PRI".equals(tmp_columnKey) ) {
	                isPrimKey = Boolean.TRUE;
	            }

                BcrudCfgColumnObj tmp_bcrudCfgColumnObj = new BcrudCfgColumnObj();
				tmp_bcrudCfgColumnObj.setColumn(tmp_columnName);
				tmp_bcrudCfgColumnObj.setColumnTypeExp(tmp_columnType);
				tmp_bcrudCfgColumnObj.setComment(tmp_columnComment); // 注释 
				tmp_bcrudCfgColumnObj.setColumnType(columnTypeStr);
				tmp_bcrudCfgColumnObj.setJdbcType(jdbcType);
				if(tmp_numericPrecision != null && tmp_numericPrecision > 0 ) {
				    tmp_charMaxLength = tmp_numericPrecision;
				    if(tmp_numericScale != null && tmp_numericScale > 0 ) {
				        tmp_charMaxLength += tmp_numericScale ;
				    }
				}
				tmp_bcrudCfgColumnObj.setColumnLength(tmp_charMaxLength);
				tmp_bcrudCfgColumnObj.setIsPrimKey(isPrimKey);

				tmp_bcrudCfgColumnObj.setJavaType(javaType);
				tmp_bcrudCfgColumnObj.setGolangType(golangType);

				tmp_bcrudCfgColumnObj.setPropName(tmp_propName);
				tmp_bcrudCfgColumnObj.setIsAutoIncrement(isAutoIncrement);
				
				rtList.add(tmp_bcrudCfgColumnObj);
			}
		}
		return rtList;
	}

	/**
	 * 配置表对象列表创建
	 * 创建表JsBean
	 * 
	 * @return
	 */
	@Override
	public List<BcrudCfgTableObj> doBcrudCfgTableObjListCreate(BcrudConfigBean reqCfgBean) throws Exception {
			logger.debug("开始创建 TableJsBean 列表 ");
			List<BcrudCfgTableObj> rtList = new ArrayList<>();

			List<BcrudCfgTableObj> lmtDataTables = reqCfgBean.getCfgTables(); // 配置限制的表.
			Connection currConnection = connectionGenerate(reqCfgBean);

			// 提取本数据库名称
			String databaseName = CodecDbConnectionUtils.getDatabaseName(currConnection);
			
			logger.debug("数据库名称:" + databaseName);
			List<String> allTableNameList = getAllTablesNameList(currConnection, databaseName);
			Map<String,String> allTableNameMap = allTableNameList.stream().collect(Collectors.toMap(obj->obj, obj->obj)); 
			logger.debug("所有表的名称:" + allTableNameList);

			// 计算匹配需要生成的表
			List<BcrudCfgTableObj> toCreateTableCfgList = new ArrayList<>();
			String dft_tableAlias = "t";
			if(lmtDataTables != null && !lmtDataTables.isEmpty() ) {
				for (BcrudCfgTableObj tmpCfgTableObj : lmtDataTables) {
					String tmp_tableName = tmpCfgTableObj.getTableName();
					if( allTableNameMap.get(tmp_tableName) == null ) {
						throw new IllegalArgumentException("库表不存在(" +  tmp_tableName + ")");
					}
					toCreateTableCfgList.add(tmpCfgTableObj);
				}
			}else {
				// 生成所有且默认配置
				for (String tmpTableName : allTableNameList) {
					BcrudCfgTableObj tmp_cfgTable = new BcrudCfgTableObj(tmpTableName,dft_tableAlias);
					toCreateTableCfgList.add(tmp_cfgTable);
				}
			}

			for (BcrudCfgTableObj tmp_tableCfg : toCreateTableCfgList) {
				BcrudCfgTableObj tableJsBean = doBcrudCfgTableObjAndColumnsCalcAndFill(reqCfgBean,currConnection , databaseName, tmp_tableCfg );
				rtList.add(tableJsBean);
			}

			return rtList;
	}


//	/**
//	 * 创建JSON文件到输出目录
//	 * 
//	 * @param cfgBean
//	 * @param tableJsBeans
//	 * @return
//	 * @throws IOException
//	 */
//	@Override
//	public List<String> doCfgTableObjListOutputToJsonFiles(BcrudConfigBean cfgBean, List<BcrudCfgTableObj> tableJsBeans) throws Exception {
//		List<String> filePathList = new ArrayList<>();
//
//		if (tableJsBeans == null || tableJsBeans.size() < 1) {
//			return filePathList;
//		}
//		String jsonFileOutDir = cfgBean.getJsonFileOutDir();
//		File jsonFileOutDirFile = new File(jsonFileOutDir);
//		if (!jsonFileOutDirFile.exists()) {
//			jsonFileOutDirFile.mkdirs();
//		}
//
//		for (BcrudCfgTableObj tableJsonMap : tableJsBeans) {
//			String jsonFileName = tableJsonMap.getTableName() + ".json";
//			logger.debug("生成后的json文件名为:" + jsonFileName);
//			String jsonStr = JSONObject.toJSONString(tableJsonMap, true);
//			String outFilePath = jsonFileOutDir + jsonFileName;
//			FileWriter fwriter = new FileWriter(outFilePath);
//			fwriter.write(jsonStr);
//			fwriter.flush();
//			fwriter.close();
//			filePathList.add(outFilePath);
//		}
//		return filePathList;
//	}

//	/**
//	 * 获取数据库名称
//	 * @param conection
//	 * @param dbdialect 
//	 * @return
//	 * @throws SQLException 
//	 */
//	private String getDatabaseName(Connection conection) throws SQLException{
//		String rtStr = conection.getCatalog();
//		return rtStr ;
//	}

	/**
	 * 获取所有表名称
	 * @param connection
	 * @param dbName 
	 * @return
	 * @throws SQLException
	 */
	private List<String> getAllTablesNameList(Connection connection, String dbName ) throws SQLException{
		
		// mysql 获取所有表名的方式 , SELECT  t.* FROM  `information_schema`.`TABLES` t WHERE 1=1 AND t.`TABLE_SCHEMA` = '<tableName>'  ORDER BY t.`TABLE_NAME` ASC  
		String calcSql = " SELECT  t.* FROM  `information_schema`.`TABLES` t WHERE 1=1 AND t.`TABLE_SCHEMA` = '"+dbName+"' "
				+ " "
				+ " ORDER BY t.`TABLE_NAME` ASC "
				;
		ResultSet descRst = connection.createStatement().executeQuery(calcSql);
		List<String> rtList = new ArrayList<String>();
		Map<String,String> existTableNameMap = new HashMap<>();
		while(descRst.next()) {
			String tmp_tableName = descRst.getString("TABLE_NAME"); // 表名称
//			String tmp_tableComment = descRst.getString("TABLE_COMMENT"); // 表注注释

			if(tmp_tableName != null && !"".equals(tmp_tableName) && existTableNameMap.get(tmp_tableName) == null ){
				existTableNameMap.put(tmp_tableName, tmp_tableName); 
				rtList.add(tmp_tableName);
			}
		}
		return rtList ;
	}

}
